package com.liliya.servlet.admin;

import com.liliya.pojo.User;
import com.liliya.service.UserService;
import com.liliya.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2021/1/5 15:40
 */

@WebServlet(urlPatterns = "/admin/login")
public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        HttpSession session = req.getSession();
        session.setAttribute("username", username);
        User user = userService.findUserByName(username);
        if (user==null || !user.getUsername().equals(username)) {
            session.setAttribute("loginmsg", "用户名不存在");
            resp.sendRedirect(req.getContextPath() + "/admin/adminlogin.jsp");
        } else if (!user.getPassword().equals(password)) {
            session.setAttribute("loginmsg", "密码错误");
            resp.sendRedirect(req.getContextPath() + "/admin/adminlogin.jsp");
        } else {
            session.setAttribute("user",user);
            resp.sendRedirect(req.getContextPath() + "/loginsuccess.jsp");
        }
        System.out.println(username + ":" + password);

    }
}